package my.market.display.goods;

import my.market.jdbc.dataClass.Goods;
import my.market.jdbc.dao.GoodsDao;
import java.util.List;
import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.cj.x.protobuf.MysqlxPrepare.Prepare.Builder;

/**
 * Servlet implementation class displaygoods
 */
@WebServlet("/displaygoods")
public class DisplayGoods extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DisplayGoods() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String typeString = request.getParameter("type");
		String valuString = request.getParameter("textfield");
		GoodsDao gDao = new GoodsDao();
		List<Goods> goods = null; 
		if (typeString != null) {
			try {
				goods = gDao.searchGoodsByType(typeString);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else {
			if(valuString == null) {
				try {
					goods = gDao.searchAll(); 
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}else {
				try {
					valuString = new String(valuString.getBytes("iso8859-1"), "utf-8");
					goods = gDao.findGood(valuString); 
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		request.setAttribute("batch_goods", goods);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
